package com.ht.module.bus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ht.abnormal.HtException;
import com.ht.config.dataAuth.DataScope;
import com.ht.constant.BusConstant;
import com.ht.module.bus.entity.TStudent;
import com.ht.module.bus.mapper.TStudentMapper;
import com.ht.module.bus.service.TStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.util.Pager;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hejialun
 * @since 2024-07-29
 */
@Service
public class TStudentServiceImpl extends ServiceImpl<TStudentMapper, TStudent> implements TStudentService {

    @Override
    public Pager<TStudent> findPageAuth(Pager<TStudent> pager, TStudent en) {
        QueryWrapper<TStudent> qw = getQW(en);
        return baseMapper.findPageAuth(pager,qw,new DataScope().initOrder(pager));
    }

    @Override
    public void add(TStudent en) {
        Integer i = baseMapper.selectCount(
                new QueryWrapper<TStudent>()
                        .eq("identity_card_number", en.getIdentityCardNumber())
        );
        if(i>0){
            throw new HtException("当前身份证号的学生已经存在！");
        }
        baseMapper.insert(en);
    }

    @Override
    public void editById(TStudent en) {
        Integer i = baseMapper.selectCount(
                new QueryWrapper<TStudent>()
                        .eq("identity_card_number", en.getIdentityCardNumber())
                        .ne("student_code",en.getStudentCode())
        );
        if(i>0){
            throw new HtException("当前身份证号的学生已经存在！");
        }
        baseMapper.updateById(en);
    }

    private QueryWrapper<TStudent> getQW(TStudent en){
        QueryWrapper<TStudent> qw=new QueryWrapper<>();
        if(StrUtil.isNotEmpty(en.getSchoolName())){
            qw.like("tab.school_name",en.getSchoolName());
        }
        if(StrUtil.isNotEmpty(en.getStudentFulllName())){
            qw.like("tab.student_fulll_name",en.getStudentFulllName());
        }
        if(StrUtil.isNotEmpty(en.getIdentityCardNumber())){
            qw.eq("tab.identity_card_number",en.getIdentityCardNumber());
        }
        if(StrUtil.isNotEmpty(en.getDistrictCode())){
            qw.eq("tab.district_code",en.getDistrictCode());
        }
        qw.eq("del_flag", BusConstant.DEL_FLAG_N);
        qw.orderByDesc("tab.create_date");
        return qw;
    }
}
